<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::ActiveRecord::UUID</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Module</span>
            Hoodoo::ActiveRecord::UUID
        </h1>
        <ul class="files">
            <li><a href="../../../files/lib/hoodoo/active/active_record/uuid_rb.html">lib/hoodoo/active/active_record/uuid.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p><a href="Support.html">Support</a> mixin for models subclassed from <a
href="Base.html">ActiveRecord::Base</a> providing automatic <a
href="UUID.html">UUID</a> management. See:</p>
<ul><li>
<p><a
href="http://guides.rubyonrails.org/active_record_basics.html">guides.rubyonrails.org/active_record_basics.html</a></p>
</li></ul>

<p>By including this module, an on-create validation is added to the including
model which assigns a <a href="UUID.html">UUID</a> if none is currently set
(<code>id</code> is <code>nil</code>). It also adds validations to ensure
the <code>id</code> is present, unique and a valid <a
href="UUID.html">UUID</a>. You should always make sure that there are
accompanying database-level uniqueness and non-null constraints on the
relevant table&#39;s `id` column, too.</p>

<p><strong>IMPORTANT:</strong> See <a
href="UUID.html#method-c-included">::included</a> for important information
about database requirements / table creation when using this mixin.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>I</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-included">included</a>,
              </li>
              <li>
                <a href="#method-c-instantiate">instantiate</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-included">
              <b>included</b>( model )
            <a href="../../../classes/Hoodoo/ActiveRecord/UUID.html#method-c-included" name="method-c-included" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Instantiates this module when it is included.</p>

<p>Example:</p>

<pre><code>class SomeModel &lt; ActiveRecord::Base
  include Hoodoo::ActiveRecord::UUID
  # ...
end
</code></pre>
<dl class="rdoc-list note-list"><dt><code>model</code>
<dd>
<p>The <a href="Base.html">ActiveRecord::Base</a> descendant class that is
including this module.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-included_source')" id="l_method-c-included_source">show</a>
              </p>
              <div id="method-c-included_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/active/active_record/uuid.rb, line 44</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">included</span>( <span class="ruby-identifier">model</span> )
  <span class="ruby-identifier">instantiate</span>( <span class="ruby-identifier">model</span> ) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">model</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-keyword">super</span>( <span class="ruby-identifier">model</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-instantiate">
              <b>instantiate</b>( model )
            <a href="../../../classes/Hoodoo/ActiveRecord/UUID.html#method-c-instantiate" name="method-c-instantiate" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>When called, this method:</p>
<ul><li>
<p>Declares &#39;id&#39; as the primary key</p>
</li><li>
<p>Self-assigns a <a href="UUID.html">UUID</a> to &#39;id&#39; via an
on-create validation</p>
</li><li>
<p>Adds validations to &#39;id&#39; to ensure it is present, unique and a
valid <a href="UUID.html">UUID</a>.</p>
</li></ul>

<p>The model <strong>MUST</strong> define its database representation in
migrations so that <code>id</code> is a string based primary key, as
follows:</p>

<pre><code>create_table :model_table_name, :id =&gt; :string do | t |
  # ...your normal column definitions go here...
end

change_column :model_table_name, :id, :string, :limit =&gt; 32
</code></pre>
<dl class="rdoc-list note-list"><dt><code>model</code>
<dd>
<p>The <a href="Base.html">ActiveRecord::Base</a> descendant class that is
including this module.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-instantiate_source')" id="l_method-c-instantiate_source">show</a>
              </p>
              <div id="method-c-instantiate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/active/active_record/uuid.rb, line 68</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">instantiate</span>( <span class="ruby-identifier">model</span> )
  <span class="ruby-identifier">model</span>.<span class="ruby-identifier">primary_key</span> = <span class="ruby-string">&#39;id&#39;</span>

  <span class="ruby-identifier">model</span>.<span class="ruby-identifier">validate</span>( <span class="ruby-value">:on</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">:create</span> ) <span class="ruby-keyword">do</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">id</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">UUID</span>.<span class="ruby-identifier">generate</span>()
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">model</span>.<span class="ruby-identifier">validates</span>(
    <span class="ruby-value">:id</span>,
    {
      <span class="ruby-value">:uuid</span>       =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>,
      <span class="ruby-value">:presence</span>   =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>,
      <span class="ruby-value">:uniqueness</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>
    }
  )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
